package de.au.fi.domain;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.validation.constraints.NotNull;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.ektorp.support.CouchDbDocument;
import org.ektorp.support.TypeDiscriminator;
import org.hibernate.validator.constraints.NotEmpty;
import org.junit.Ignore;
import org.junit.Test;

import de.au.fi.calc.CalcHelper;
import de.au.fi.calc.results.Result;
import de.au.fi.calc.results.ResultFactory;

public class StockResult extends CouchDbDocument{
	
	private static final long serialVersionUID = 1L;

	@JsonIgnore
	Result result;
	
	@NotNull
	@NotEmpty
	@TypeDiscriminator
	String wkn;
	
	String isin;
	
	@NotNull
	@NotEmpty
	String name;
	
	CapType capType;
	
	Double marktKapInMio;

	Double roeLj;
	
	Double ebitLj;
	
	Double ebitdaLj;
	
	public Double getEbitdaLj() {
		return ebitdaLj;
	}


	public void setEbitdaLj(Double ebitdaLj) {
		this.ebitdaLj = ebitdaLj;
	}


	Double eigKapQuLj;
	
	Double kgvAj;	
	Double kgv1j;
	Double kgv2j;
	Double kgv3j;
	Double kgv4j;
	
	AnalystenMeinung analystenMeinung;
	
	Double reaktionQz;
	
	Double gewinnRev;
	
	Double kursNow;
	
	Double kurs6monate;
	
	Double kurs1jahr;
	
	DreimonatsReversal dreimonatsReversal;
	
	Double epsAj;
	
	Double epsNj;
	
	Double dividende;

	private String onVistaId;
	
	private boolean magicFormula = false;

	private String ticker;

	private Double piotroskiScore;
	
	private Double altMannZScore;
	
	public enum CapType
	{
		LARGE,
		MID,
		SMALL,
		FINANCE;
	}
	
	public enum DreimonatsReversal
	{
		BESSER_ALS_DAX,
		GLEICH_DAX,
		SCHLECHTER_ALS_DAX;
	}
	
	public enum AnalystenMeinung
	{
		SELL,
		BUY,
		HOLD;
	}
	
	@Override
	public String getId() {
		return this.wkn;
	}
	

	@Override
	public void setRevision(String s) {
		// downstream code does not like revision set to emtpy string, which Spring does when binding
		if (s != null && !s.isEmpty()) super.setRevision(s);
	}

	public Double getRoeLj() {
		return roeLj;
	}

	public void setRoeLj(Double roeLj) {
		this.roeLj = roeLj;
	}

	public Double getEbitLj() {
		return ebitLj;
	}

	public void setEbitLj(Double ebitLj) {
		this.ebitLj = ebitLj;
	}

	public Double getEigKapQuLj() {
		return eigKapQuLj;
	}

	public void setEigKapQuLj(Double eigKapQuLj) {
		this.eigKapQuLj = eigKapQuLj;
	}

	public Double getKgvAj() {
		return kgvAj;
	}

	public void setKgvAj(Double kgvAj) {
		this.kgvAj = kgvAj;
	}

	public AnalystenMeinung getAnalystenMeinung() {
		return analystenMeinung;
	}

	public void setAnalystenMeinung(AnalystenMeinung analystenMeinung) {
		this.analystenMeinung = analystenMeinung;
	}

	public Double getReaktionQz() {
		return reaktionQz;
	}

	public void setReaktionQz(Double reaktionQz) {
		this.reaktionQz = reaktionQz;
	}

	public Double getGewinnRev() {
		return gewinnRev;
	}

	public void setGewinnRev(Double gewinnRev) {
		this.gewinnRev = gewinnRev;
	}

	public Double getKursNow() {
		return kursNow;
	}

	public void setKursNow(Double kursNow) {
		this.kursNow = kursNow;
	}

	public Double getKurs6monate() {
		return kurs6monate;
	}

	public void setKurs6monate(Double kurs6monate) {
		this.kurs6monate = kurs6monate;
	}

	public Double getKurs1jahr() {
		return kurs1jahr;
	}

	public void setKurs1jahr(Double kurs1jahr) {
		this.kurs1jahr = kurs1jahr;
	}

	public DreimonatsReversal getDreimonatsReversal() {
		return dreimonatsReversal;
	}

	public void setDreimonatsReversal(DreimonatsReversal dreimonatsReversal) {
		this.dreimonatsReversal = dreimonatsReversal;
	}
	
	public Double getEpsAj() {
		return epsAj;
	}

	public void setEpsAj(Double epsAj) {
		this.epsAj = epsAj;
	}

	public Double getEpsNj() {
		return epsNj;
	}

	public void setEpsNj(Double epsNj) {
		this.epsNj = epsNj;
	}

	public Double getDividende() {
		return dividende;
	}

	public void setDividende(Double dividende) {
		this.dividende = dividende;
	}

	public String getWkn() {
		return wkn;
	}

	public void setWkn(String wkn) {
		this.wkn = wkn;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	

	public Double getKgv1j() {
		return kgv1j;
	}

	public void setKgv1j(Double kgv1j) {
		this.kgv1j = kgv1j;
	}

	public Double getKgv2j() {
		return kgv2j;
	}

	public void setKgv2j(Double kgv2j) {
		this.kgv2j = kgv2j;
	}

	public Double getKgv3j() {
		return kgv3j;
	}

	public void setKgv3j(Double kgv3j) {
		this.kgv3j = kgv3j;
	}

	public Double getKgv4j() {
		return kgv4j;
	}

	public void setKgv4j(Double kgv4j) {
		this.kgv4j = kgv4j;
	}

	public CapType getCapType() {
		return capType;
	}

	public boolean isMagicFormula() {
		return magicFormula;
	}


	public void setMagicFormula(boolean magicFormula) {
		this.magicFormula = magicFormula;
	}


	public void setCapType(CapType capType) {
		this.capType = capType;
	}

	public void setResult(Result result) {
		this.result = result;
	}


	public Double getMarktKapInMio() {
		return marktKapInMio;
	}


	public void setMarktKapInMio(Double marktKapInMio) {
		this.marktKapInMio = marktKapInMio;
	}


	public String getIsin() {
		return isin;
	}


	public void setIsin(String isin) {
		this.isin = isin;
	}


	public void setCapTypeByMarktKap() {
		if(capType == CapType.FINANCE)return;
		if(marktKapInMio != null)
		{
			if(marktKapInMio >= 5000)capType = CapType.LARGE;
			else if(marktKapInMio < 5000 && marktKapInMio > 500)
				this.capType = CapType.MID;
			else capType = CapType.SMALL;
		}
	}
	
	public void setOnVistaId(String onVistaId) {
		this.onVistaId = onVistaId;
	}


	public String getOnVistaId() {
		return onVistaId;
	}
	
	@JsonIgnore
	public Double getDividendeAsPercent()
	{
		if(dividende==null || dividende == 0.0 || kursNow == null || kursNow == 0.0)return null;
		return CalcHelper.roundToTwoDigits(dividende / kursNow * 100);
	}
	
	public String getTicker()
	{
		return ticker;
	}

	public void setTicker(String ticker) {
		this.ticker = ticker;
	}


	public void setPiotroskiScore(Double piotri) {
		this.piotroskiScore = piotri;
		
	}


	public Double getAltMannZScore() {
		return altMannZScore;
	}


	public void setAltMannZScore(Double altMannZScore) {
		this.altMannZScore = altMannZScore;
	}


	public Double getPiotroskiScore() {
		return piotroskiScore;
	}
}
